/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.alan.hyperblobs;

import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.Contact;

import java.util.*;

/**
 *
 * @author Michael Zhang
 */
public class LevelPhysicsUpdater implements Runnable, ContactListener {

    /**
     * The amount of time to be emulated each run
     */
    private static final float TIME_STEP = 1 / 60;
    /**
     * Our world
     */
    private final World mWorld;
    /**
     * Our receiver for updates
     */
    private final ILevelUpdates mUpdateReceiver;
    /**
     * Our data to write to
     */
    private final Map mData;

    /**
     *
     * @param gravity The gravity of the world
     * @param receiver The receiver for physics events
     * @param data The {@code Map} tow write location data to 
     */
    public LevelPhysicsUpdater(float gravity, ILevelUpdates receiver, Map data) {
        mUpdateReceiver = receiver;
        mWorld = new World(new Vec2(0.0f, gravity), true);
        registerListeners();
        mData = data;
    }

    /**
     * Registers listeners
     */
    private void registerListeners() {
        mWorld.setContactListener(this);
    }

    /**
     * Updates physics and collisions.
     */
    public void run() {
        mWorld.step(TIME_STEP, 8, 3);
    }

    /**
     * {@inheritDoc}
     *
     * @param arg0 {@inheritDoc}
     */
    public void beginContact(Contact arg0) {
    }

    /**
     * {@inheritDoc}
     *
     * @param arg0 {@inheritDoc}
     */
    public void endContact(Contact arg0) {
    }

    /**
     * {@inheritDoc}
     *
     * @param arg0 {@inheritDoc}
     * @param arg1 {@inheritDoc}
     */
    public void preSolve(Contact arg0, Manifold arg1) {
    }

    /**
     * {@inheritDoc}
     *
     * @param arg0 {@inheritDoc}
     * @param arg1 {@inheritDoc}
     */
    public void postSolve(Contact arg0, ContactImpulse arg1) {
    }
}
